package org.bpms.controller.admin;

import java.io.File;
import java.io.FileInputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

import org.bpms.model.admin.Book;
import org.bpms.model.admin.Booklog;
import org.bpms.model.admin.Booktemp;
import org.bpms.model.admin.Btype;
import org.bpms.model.admin.Srole;
import org.bpms.model.admin.Suser;
import org.bpms.service.admin.BookService;
import org.bpms.service.admin.BooklogService;
import org.bpms.service.admin.BooktempService;
import org.bpms.service.admin.BookversionService;
import org.bpms.service.admin.RoleService;
import org.bpms.service.admin.TypeService;
import org.bpms.util.Compress;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jfinal.aop.Enhancer;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.upload.UploadFile;

/**
 * 归档控制器
 * @author hupei
 *
 */
public class BookController extends Controller{
	private BookService bookService = Enhancer.enhance(BookService.class);
	private BooklogService booklogService = Enhancer.enhance(BooklogService.class);
	private BooktempService booktempService = Enhancer.enhance(BooktempService.class);
	private BookversionService bookversionService = Enhancer.enhance(BookversionService.class);
	private RoleService roleService = Enhancer.enhance(RoleService.class);
	private TypeService typeService = Enhancer.enhance(TypeService.class);
	
	
	/**
	 * 已提交
	 */
	public void submit(){
		Map<String,Object> pagemap = new HashMap<String, Object>();
		String tabid = getPara("tabid");
		Suser user = getSessionAttr("user");
		String file_number = getPara("file_number" , "");
		String flow_code = getPara("flow_code" , "");
		String title = getPara("title" , "");
		Integer pageNumber = getParaToInt("pageCurrent", 1);//要访问第几页，默认1
		Integer pageSize = getParaToInt("pageSize", 100);//每页多少条数据，默认15
		String orderField = getPara("orderField", "ctime");//对什么字段排序，默认空字符串
		String orderDirection = getPara("orderDirection", "desc");//升序还是降序，默认升序
		List<Srole> ls = roleService.getRoleFromUser(user.getStr("id"));
		String role = "";
		if (null != ls && ls.size() >0){
			role = ls.get(0).getStr("text");
		}
		bookService.findAllSubmit(role , flow_code , title, file_number, 
				user.getStr("id"), pageNumber, pageSize, orderField, orderDirection, pagemap);
		setAttr("title", title);
		setAttr("file_number", file_number);
		setAttr("flow_code", flow_code);
		setAttr("tabid", tabid);
		setAttr("role", role);
		if  (role.equals("资源开发岗")){
			List<Book> lb1 = (ArrayList)pagemap.get("pageData");
			if (null != lb1 && lb1.size() > 0){
				for (Book b:lb1){
					Booklog log = Booklog.me.findFirst("select * from booklog where bookid=? and dotype='退回' order by createtime desc" , b.get("id"));
					if (null != log && log.get("dotype").equals("退回")){
						if (null != log.get("reason")){
							b.setReson(log.getStr("reason"));
							b.setResonpea(log.getStr("creater"));
						}
					}
				}
			}
			setAttr("page",pagemap);
			render("/admin/book/sbookk.html");
		}else if  (role.equals("科领导")){
			List<Book> lb1 = (ArrayList)pagemap.get("pageData");
			if (null != lb1 && lb1.size() > 0){
				for (Book b:lb1){
					Booklog log = Booklog.me.findFirst("select * from booklog where bookid=? and dotype='退回' order by createtime desc" , b.get("id"));
					if (null != log && log.get("dotype").equals("退回")){
						if (null != log.get("reason")){
							b.setReson(log.getStr("reason"));
							b.setResonpea(log.getStr("creater"));
						}
					}
				}
			}
			setAttr("page",pagemap);
			render("/admin/book/sbookk.html");
		}else if  (role.equals("资源收集岗")){
			List<Book> lb1 = (ArrayList)pagemap.get("pageData");
			if (null != lb1 && lb1.size() > 0){
				for (Book b:lb1){
					Booklog log = Booklog.me.findFirst("select * from booklog where bookid=? and dotype='退回' order by createtime desc" , b.get("id"));
					if (null != log && log.get("dotype").equals("退回")){
						if (null != log.get("reason")){
							b.setReson(log.getStr("reason"));
							b.setResonpea(log.getStr("creater"));
						}
					}
				}
			}				
			setAttr("page",pagemap);
			render("/admin/book/sbootj.html");
		}		
	}
	
	/**
	 * 待接收的
	 */
	public void waitget(){
		Map<String,Object> pagemap = new HashMap<String, Object>();
		String tabid = getPara("tabid");
		Suser user = getSessionAttr("user");
		String file_number = getPara("file_number" , "");
		String flow_code = getPara("flow_code" , "");
		String title = getPara("title" , "");
		Integer pageNumber = getParaToInt("pageCurrent", 1);//要访问第几页，默认1
		Integer pageSize = getParaToInt("pageSize", 100);//每页多少条数据，默认15
		String orderField = getPara("orderField", "ctime");//对什么字段排序，默认空字符串
		String orderDirection = getPara("orderDirection", "desc");//升序还是降序，默认升序
		List<Srole> ls = roleService.getRoleFromUser(user.getStr("id"));
		List<Btype> lb = typeService.getTypeFromUser(user.getStr("id"));
		String role = "";
		if (null != ls && ls.size() >0){
			role = ls.get(0).getStr("text");
		}
		bookService.findAllWaitGet(lb , role , flow_code , title, file_number, 
				user.getStr("id"), pageNumber, pageSize, orderField, orderDirection, pagemap);
		setAttr("title", title);
		setAttr("file_number", file_number);
		setAttr("flow_code", flow_code);
		setAttr("tabid", tabid);
		setAttr("role", role);
		if  (role.equals("资源开发岗")){
			List<Book> lb1 = (ArrayList)pagemap.get("pageData");
			if (null != lb1 && lb1.size() > 0){
				for (Book b:lb1){
					Booklog log = Booklog.me.findFirst("select * from booklog where bookid=? and dotype='退回' order by createtime desc" , b.get("id"));
					if (null != log && log.get("dotype").equals("退回")){
						if (null != log.get("reason")){
							b.setReson(log.getStr("reason"));
							b.setResonpea(log.getStr("creater"));
						}
					}
				}
			}	
			setAttr("page",pagemap);
			render("/admin/book/sboodd.html");
		}else if  (role.equals("科领导")){
			List<Book> lb1 = (ArrayList)pagemap.get("pageData");
			if (null != lb1 && lb1.size() > 0){
				for (Book b:lb1){
					Booklog log = Booklog.me.findFirst("select * from booklog where bookid=? and dotype='退回' order by createtime desc" , b.get("id"));
					if (null != log && log.get("dotype").equals("退回")){
						if (null != log.get("reason")){
							b.setReson(log.getStr("reason"));
							b.setResonpea(log.getStr("creater"));
						}
					}
				}
			}	
			setAttr("page",pagemap);			
			render("/admin/book/sboodd.html");
		}else if  (role.equals("资源收集岗")){
			render("/admin/book/sbookk.html");
		}else if  (role.equals("资源利用岗")){
			List<Book> lb1 = (ArrayList)pagemap.get("pageData");
			if (null != lb1 && lb1.size() > 0){
				for (Book b:lb1){
					Booklog log = Booklog.me.findFirst("select * from booklog where bookid=? and dotype='退回' order by createtime desc" , b.get("id"));
					if (null != log && log.get("dotype").equals("退回")){
						if (null != log.get("reason")){
							b.setReson(log.getStr("reason"));
							b.setResonpea(log.getStr("creater"));
						}
					}
				}
			}	
			setAttr("page",pagemap);			
			render("/admin/book/sbooll.html");
		}		
	}
	
	/**
	 * 等待处理的
	 */
	public void waitDo(){
		Map<String,Object> pagemap = new HashMap<String, Object>();
		String tabid = getPara("tabid");
		Suser user = getSessionAttr("user");
		String file_number = getPara("file_number" , "");
		String flow_code = getPara("flow_code" , "");
		String title = getPara("title" , "");
		Integer pageNumber = getParaToInt("pageCurrent", 1);//要访问第几页，默认1
		Integer pageSize = getParaToInt("pageSize", 100);//每页多少条数据，默认15
		String orderField = getPara("orderField", "ctime");//对什么字段排序，默认空字符串
		String orderDirection = getPara("orderDirection", "desc");//升序还是降序，默认升序
		List<Srole> ls = roleService.getRoleFromUser(user.getStr("id"));
		String role = "";
		if (null != ls && ls.size() >0){
			role = ls.get(0).getStr("text");
		}
		bookService.findAllWait(role , title, flow_code , file_number, 
				user.getStr("id"), pageNumber, pageSize, orderField, orderDirection, pagemap);
		setAttr("title", title);
		setAttr("file_number", file_number);
		setAttr("flow_code", flow_code);
		setAttr("tabid", tabid);
		setAttr("page",pagemap);
		setAttr("role", role);
		if  (role.equals("资源开发岗")){
			render("/admin/book/wbookk.html");
		}else if  (role.equals("科领导")){
			render("/admin/book/wbookk.html");
		}		
	}
	
	/**
	 * 正在处理的
	 */
	public void handle(){
		Map<String,Object> pagemap = new HashMap<String, Object>();
		String tabid = getPara("tabid");
		Suser user = getSessionAttr("user");
		String file_number = getPara("file_number" , "");
		String flow_code = getPara("flow_code" , "");
		String title = getPara("title" , "");
		Integer pageNumber = getParaToInt("pageCurrent", 1);//要访问第几页，默认1
		Integer pageSize = getParaToInt("pageSize", 100);//每页多少条数据，默认15
		String orderField = getPara("orderField", "ctime");//对什么字段排序，默认空字符串
		String orderDirection = getPara("orderDirection", "desc");//升序还是降序，默认升序
		List<Srole> ls = roleService.getRoleFromUser(user.getStr("id"));
		String role = "";
		if (null != ls && ls.size() >0){
			role = ls.get(0).getStr("text");
		}
		bookService.findAllDoing(role , title, flow_code, file_number, 
				user.getStr("id"), pageNumber, pageSize, orderField, orderDirection, pagemap);
		setAttr("title", title);
		setAttr("file_number", file_number);
		setAttr("flow_code", flow_code);
		setAttr("tabid", tabid);
		setAttr("role", role);
		if (role.equals("资源收集岗")){
			List<Book> lb = (ArrayList)pagemap.get("pageData");
			if (null != lb && lb.size() > 0){
				for (Book b:lb){
					Booklog log = Booklog.me.findFirst("select * from booklog where bookid=? and dotype='退回' order by createtime desc" , b.get("id"));
					if (null != log && log.get("dotype").equals("退回")){
						if (null != log.get("reason")){
							b.setReson(log.getStr("reason"));
							b.setResonpea(log.getStr("creater"));
						}
					}
				}
			}
			setAttr("page",pagemap);
			render("/admin/book/lbooks.html");
		}else if  (role.equals("资源开发岗")){
			List<Book> lb = (ArrayList)pagemap.get("pageData");
			if (null != lb && lb.size() > 0){
				for (Book b:lb){
					Booklog log = Booklog.me.findFirst("select * from booklog where bookid=? and dotype='退回' order by createtime desc" , b.get("id"));
					if (null != log && log.get("dotype").equals("退回")){
						if (null != log.get("reason")){
							b.setReson(log.getStr("reason"));
							b.setResonpea(log.getStr("creater"));
						}
					}
				}
			}			
			setAttr("page",pagemap);
			render("/admin/book/lbookk.html");
		}else if  (role.equals("资源利用岗")){
			setAttr("page",pagemap);
			render("/admin/book/lbookl.html");
		}else if  (role.equals("科领导")){
			List<Book> lb = (ArrayList)pagemap.get("pageData");
			if (null != lb && lb.size() > 0){
				for (Book b:lb){
					Booklog log = Booklog.me.findFirst("select * from booklog where bookid=? and dotype='退回' order by createtime desc" , b.get("id"));
					if (null != log && log.get("dotype").equals("退回")){
						if (null != log.get("reason")){
							b.setReson(log.getStr("reason"));
							b.setResonpea(log.getStr("creater"));
						}
					}
				}
			}			
			setAttr("page",pagemap);
			render("/admin/book/lbookk.html");
		}
	}
	
	/**
	 * 上传文件---A节点
	 */
	public void deploy(){
		Map map = new HashMap();
		Suser user = getSessionAttr("user");
		List<UploadFile> lf = getFiles();
		int random = randomInt();
		List<Book> lb = new ArrayList<Book>();
		for (UploadFile file : lf){
			File f = file.getFile();
			String path = f.getAbsolutePath();//绝对路径
			String fileExt = path.substring(path.lastIndexOf(".") + 1);//扩展名
			if (null != fileExt && (fileExt.equals("xls"))){
				reportXls(lb , f);
				f.delete();
			}else if(null != fileExt && fileExt.equals("rar")){
				String reportpath = reportRar(lb , f);
				f.delete();
				deleteDirectory(reportpath);
			}else if(null != fileExt && fileExt.equals("zip")){
				String reportpath = reportZip(lb , f);
				f.delete();
				deleteDirectory(reportpath);
			}
		}
		List<Booktemp> ltemp = null;
		if (lb.size() > 0){
			for (Book b : lb){
				Booktemp temp = new Booktemp();
				temp.setAttrs(b.getAll());
				temp.set("tempid", random);
				booktempService.save(temp);
			}
			ltemp = booktempService.findFromrandom(random , "A" , user.getStr("id"));
		}
		if (null != ltemp && ltemp.size()>0){
			map.put("random", random);
		}else{
			for (Book b : lb){
				b.set("id", null);
				b.set("state", "A");
				b.set("creater", user.get("id"));
				if (Book.me.find("select * from book where bookid='"+b.getStr("bookid")+"'").isEmpty()){
					bookService.save(b);
				}
			}
			booktempService.remove(random);
			map.put("random", 0);
		}
		renderJson(map);
	}
	
	/**
	 * 上传文件--c节点
	 */
	public void deployc(){
		Map map = new HashMap();
		Suser user = getSessionAttr("user");
		List<UploadFile> lf = getFiles();
		int random = randomInt();
		List<Book> lb = new ArrayList<Book>();
		for (UploadFile file : lf){
			File f = file.getFile();
			String path = f.getAbsolutePath();//绝对路径
			String fileExt = path.substring(path.lastIndexOf(".") + 1);//扩展名
			if (null != fileExt && (fileExt.equals("xls"))){
				reportXls(lb , f);
				f.delete();
			}else if(null != fileExt && fileExt.equals("rar")){
				String reportpath = reportRar(lb , f);
				f.delete();
				deleteDirectory(reportpath);
			}else if(null != fileExt && fileExt.equals("zip")){
				String reportpath = reportZip(lb , f);
				f.delete();
				deleteDirectory(reportpath);
			}
		}
		List<Booktemp> ltemp = null;
		if (lb.size() > 0){
			for (Book b : lb){
				Booktemp temp = new Booktemp();
				temp.setAttrs(b.getAll());
				temp.set("tempid", random);
				booktempService.save(temp);
			}
			ltemp = booktempService.findFromrandom(random , "C" , user.getStr("id"));
		}
		if (null != ltemp && ltemp.size()>0){
			map.put("random", random);
		}else{
			for (Book b : lb){
				b.set("id", null);
				b.set("state", "C");
				b.set("creater", user.get("id"));
				if (Book.me.find("select * from book where bookid='"+b.getStr("bookid")+"'").isEmpty()){
					bookService.save(b);
				}
			}
			booktempService.remove(random);
			map.put("random", 0);
		}
		renderJson(map);
	}
	
	/**
	 * 上传文件--d节点
	 */
	public void deployd(){
		Map map = new HashMap();
		Suser user = getSessionAttr("user");
		List<UploadFile> lf = getFiles();
		int random = randomInt();
		List<Book> lb = new ArrayList<Book>();
		for (UploadFile file : lf){
			File f = file.getFile();
			String path = f.getAbsolutePath();//绝对路径
			String fileExt = path.substring(path.lastIndexOf(".") + 1);//扩展名
			if (null != fileExt && (fileExt.equals("xls"))){
				reportXls(lb , f);
				f.delete();
			}else if(null != fileExt && fileExt.equals("rar")){
				String reportpath = reportRar(lb , f);
				f.delete();
				deleteDirectory(reportpath);
			}else if(null != fileExt && fileExt.equals("zip")){
				String reportpath = reportZip(lb , f);
				f.delete();
				deleteDirectory(reportpath);
			}
		}
		List<Booktemp> ltemp = null;
		if (lb.size() > 0){
			for (Book b : lb){
				Booktemp temp = new Booktemp();
				temp.setAttrs(b.getAll());
				temp.set("tempid", random);
				booktempService.save(temp);
			}
			ltemp = booktempService.findFromrandom(random , "D" , user.getStr("id"));
		}
		if (null != ltemp && ltemp.size()>0){
			map.put("random", random);
		}else{
			for (Book b : lb){
				b.set("id", null);
				b.set("state", "D");
				b.set("creater", user.get("id"));
				if (Book.me.find("select * from book where bookid='"+b.getStr("bookid")+"'").isEmpty()){
					bookService.save(b);
				}
			}
			booktempService.remove(random);
			map.put("random", 0);
		}
		renderJson(map);
	}	
	
	/**
	 * 导入xls文件
	 * @param file
	 */
	public void reportXls(List<Book> lb , File file){
		FileInputStream input =null;
		POIFSFileSystem fs = null;
		HSSFWorkbook wb = null;
		try{
			input = new FileInputStream(file);
			fs = new POIFSFileSystem(input);
			wb = new HSSFWorkbook(fs);
			int sheetnum = wb.getNumberOfSheets();
			for (int i = 0 ; i < sheetnum ; i++){
				String sheetname = wb.getSheetName(i);
				if (sheetname.contains("卷内文件") || sheetname.contains("光盘实体") || sheetname.contains("外来技术资料")){
					HSSFSheet sheet = wb.getSheetAt(i);//得到需要读取的sheet
					int rows = sheet.getLastRowNum();//得到总行数
					if (rows > 1){
						HSSFRow row = sheet.getRow(0);//获取第一行
						Map<String , Integer> map = new HashMap<String , Integer>();
						for (int j = 0 ; j < row.getLastCellNum() ; j++){//确定各个参数在那一列
							HSSFCell cell = row.getCell(j);
							String value = cell.getStringCellValue();
							match(map , j , value);
						}
						
						for (int j = 1 ; j <= rows ; j++){
							HSSFRow temp = sheet.getRow(j);
							Book book = new Book();
							load(map , book , temp);
							if(sheetname.contains("产品")){
								book.set("typeid", "a5c4d1a913d741e188d157c4d9cb933e");
							}else if(sheetname.contains("固定资产")){
								book.set("typeid", "d5eda6da3315485e9c624a630a6967e6");
							}else if(sheetname.contains("科研")){
								book.set("typeid", "c5c879f44f5d4893a24cb5655d48d837");
							}else if(sheetname.contains("外来技术")){
								book.set("typeid", "465faa0cc0974a929f96d00807966d21");
							}else if(sheetname.contains("管理")){
								book.set("typeid", "99a9213fe7e74215a482c3d42847c231");
							}else if(sheetname.contains("照片")){
								book.set("typeid", "989ea2cdb28a42a98856a0379b5a2aef");
							}else if(sheetname.contains("视频")){
								book.set("typeid", "081b594a390d4d60886221878a0082e2");
							}else if(sheetname.contains("光盘")){
								book.set("typeid", "bf3e6679c24941749128508f83b89938");
							}
							lb.add(book);
						}						
					}
				}else{
					continue;
				}
			}
			wb.close();
			fs.close();
			input.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	/**
	 * 导入rar文件
	 * @param file
	 */
	public String reportRar(List<Book> lb , File file){
		List<String> files = new ArrayList<String>();
		String path = Compress.unRarFile(files, file);
		for (String realpath : files){
			File temp = new File(realpath);
			String fileExt = temp.getAbsolutePath().substring(temp.getAbsolutePath().lastIndexOf(".")+1);
			if (temp.exists() && fileExt.equals("xls")){
				reportXls(lb , temp);
			}
		}
		return path;
	}
	
	/**
	 * 导入zip文件
	 * @param file
	 */
	public String reportZip(List<Book> lb , File file){
		List<String> files = new ArrayList<String>();
		String path = Compress.unZipFiles(files, file);
		for (String realpath : files){
			File temp = new File(realpath);
			String fileExt = temp.getAbsolutePath().substring(temp.getAbsolutePath().lastIndexOf(".")+1);
			if (temp.exists() && fileExt.equals("xls")){
				reportXls(lb , temp);
			}
		}		
		return path;
	}
	
	/**
	 * 匹配参数在哪一列
	 * @param lm
	 * @param j
	 * @param name
	 */
	private void match(Map<String , Integer> map  , int j , String name){
		if (name.equals("归档人")){
			map.put("archive_person", j);
		}else if(name.equals("归档日期")){
			map.put("filing_date", j);
		}else if(name.equals("条形码")){
			map.put("bar_code", j);
		}else if(name.equals("录入人")){
			map.put("input_human", j);
		}else if(name.equals("录入日期")){
			map.put("input_date", j);
		}else if(name.equals("保密期限")){
			map.put("secrecy_period", j);
		}else if(name.equals("定密责任")){
			map.put("given_secret", j);
		}else if(name.equals("保管期限")){
			map.put("retention_periods", j);
		}else if(name.equals("关键词")){
			map.put("key", j);
		}else if(name.equals("面积")){
			map.put("acreage", j);
		}else if(name.equals("备注") || name.equals("文字说明")){
			map.put("remarks", j);
		}else if(name.equals("原文数量")){
			map.put("original_quantity", j);
		}else if(name.equals("流程单号")){
			map.put("flow_code", j);
		}else if(name.equals("互见号")){
			map.put("number", j);
		}else if(name.equals("档号")){
			map.put("file_number", j);
		}else if(name.equals("序号") || name.equals("张号")){
			map.put("order_number", j);
		}else if(name.equals("文件编号")){
			map.put("document_number", j);
		}else if(name.equals("责任人")){
			map.put("responsible_person", j);
		}else if(name.equals("题名")){
			map.put("title", j);
		}else if(name.equals("密级")){
			map.put("secret_level", j);
		}else if(name.equals("页数")){
			map.put("pages", j);
		}else if(name.equals("形成日期")){
			map.put("create_date", j);
		}else if(name.equals("实体份数") || name.equals("份数")){
			map.put("copies", j);
		}else if(name.equals("实体当前份数")){
			map.put("current_copies", j);
		}else if(name.equals("文件类别")){
			map.put("file_type", j);
		}else if(name.equals("载体类别")){
			map.put("carrier_type", j);
		}else if(name.equals("归档单位")){
			map.put("archive_unit", j);
		}else if(name.equals("归档科组") || name.equals("专业")){
			map.put("archive_depart", j);
		}else if(name.equals("SYS_MASTER_ID")){
			map.put("master_id", j);
		}else if(name.equals("ID")){
			map.put("bookid", j);
		}else if(name.equals("设计阶段")){
			map.put("design_phase", j);
		}else if(name.equals("接收单号")){
			map.put("receive_number", j);
		}else if(name.equals("资料编号")){
			map.put("data_num", j);
		}else if(name.equals("分类号")){
			map.put("class_num", j);
		}else if(name.equals("产品代号")){
			map.put("code", j);
		}else if(name.equals("来源单位")){
			map.put("source", j);
		}else if(name.equals("光盘编号")){
			map.put("cd_num", j);
		}else if(name.equals("原光盘编号")){
			map.put("ocd_num", j);
		}else if(name.equals("文件数量")){
			map.put("files_num", j);
		}else if(name.equals("数据大小") || name.equals("容量大小")){
			map.put("data_size", j);
		}else if(name.equals("收文号")){
			map.put("receipt_no", j);
		}else if(name.equals("项目代号")){
			map.put("item_code", j);
		}else if(name.equals("照片标题")){
			map.put("photo_title", j);
		}else if(name.equals("拍摄日期") || name.equals("活动日期")){
			map.put("shooting_date", j);
		}else if(name.equals("拍摄地点") || name.equals("活动地点")){
			map.put("location", j);
		}else if(name.equals("摄影者") || name.equals("摄制人")){
			map.put("poster", j);
		}else if(name.equals("形成年度")){
			map.put("create_year", j);
		}else if(name.equals("摄制单位")){
			map.put("production_unit", j);
		}
	}
	
	/**
	 * 装载档案
	 * @param map
	 * @param book
	 * @param row
	 */
	private void load(Map<String , Integer> map , Book book , HSSFRow row){
		if (null != map.get("archive_person")){
			book.set("archive_person", row.getCell(map.get("archive_person")).getStringCellValue());
		}if(null != map.get("filing_date")){
			book.set("filing_date", row.getCell(map.get("filing_date")).getStringCellValue());
		}if(null != map.get("bar_code")){
			book.set("bar_code", row.getCell(map.get("bar_code")).getStringCellValue());
		}if(null != map.get("input_human")){
			book.set("input_human", row.getCell(map.get("input_human")).getStringCellValue());
		}if(null != map.get("input_date")){
			book.set("input_date", row.getCell(map.get("input_date")).getStringCellValue());
		}if(null != map.get("secrecy_period")){
			book.set("secrecy_period", row.getCell(map.get("secrecy_period")).getStringCellValue());
		}if(null != map.get("given_secret")){
			book.set("given_secret", row.getCell(map.get("given_secret")).getStringCellValue());
		}if(null != map.get("retention_periods")){
			book.set("retention_periods", row.getCell(map.get("retention_periods")).getStringCellValue());
		}if(null != map.get("key")){
			book.set("key", row.getCell(map.get("key")).getStringCellValue());
		}if(null != map.get("acreage")){
			book.set("acreage", row.getCell(map.get("acreage")).getStringCellValue());
		}if(null != map.get("remarks")){
			book.set("remarks", row.getCell(map.get("remarks")).getStringCellValue());
		}if(null != map.get("original_quantity")){
			book.set("original_quantity", row.getCell(map.get("original_quantity")).getStringCellValue());
		}if(null != map.get("flow_code")){
			book.set("flow_code", row.getCell(map.get("flow_code")).getStringCellValue());
		}if(null != map.get("number")){
			book.set("number", row.getCell(map.get("number")).getStringCellValue());
		}if(null != map.get("file_number")){
			book.set("file_number", row.getCell(map.get("file_number")).getStringCellValue());
		}if(null != map.get("order_number")){
			book.set("order_number", row.getCell(map.get("order_number")).getStringCellValue());
		}if(null != map.get("document_number")){
			book.set("document_number", row.getCell(map.get("document_number")).getStringCellValue());
		}if(null != map.get("responsible_person")){
			book.set("responsible_person", row.getCell(map.get("responsible_person")).getStringCellValue());
		}if(null != map.get("title")){
			book.set("title", row.getCell(map.get("title")).getStringCellValue());
		}if(null != map.get("secret_level")){
			book.set("secret_level", row.getCell(map.get("secret_level")).getStringCellValue());
		}if(null != map.get("pages")){
			book.set("pages", row.getCell(map.get("pages")).getStringCellValue());
		}if(null != map.get("create_date")){
			book.set("create_date", row.getCell(map.get("create_date")).getStringCellValue());
		}if(null != map.get("copies")){
			book.set("copies", row.getCell(map.get("copies")).getStringCellValue());
		}if(null != map.get("current_copies")){
			book.set("current_copies", row.getCell(map.get("current_copies")).getStringCellValue());
		}if(null != map.get("file_type")){
			book.set("file_type", row.getCell(map.get("file_type")).getStringCellValue());
		}if(null != map.get("carrier_type")){
			book.set("carrier_type", row.getCell(map.get("carrier_type")).getStringCellValue());
		}if(null != map.get("archive_unit")){
			book.set("archive_unit", row.getCell(map.get("archive_unit")).getStringCellValue());
		}if(null != map.get("archive_depart")){
			book.set("archive_depart", row.getCell(map.get("archive_depart")).getStringCellValue());
		}if(null != map.get("master_id")){
			book.set("master_id", row.getCell(map.get("master_id")).getStringCellValue());
		}if(null != map.get("bookid")){
			book.set("bookid", row.getCell(map.get("bookid")).getStringCellValue());
		}if(null != map.get("design_phase")){
			book.set("design_phase", row.getCell(map.get("design_phase")).getStringCellValue());
		}if(null != map.get("receive_number")){
			book.set("receive_number", row.getCell(map.get("receive_number")).getStringCellValue());
		}if(null != map.get("data_num")){
			book.set("data_num", row.getCell(map.get("data_num")).getStringCellValue());
		}if(null != map.get("class_num")){
			book.set("class_num", row.getCell(map.get("class_num")).getStringCellValue());
		}if(null != map.get("code")){
			book.set("code", row.getCell(map.get("code")).getStringCellValue());
		}if(null != map.get("source")){
			book.set("source", row.getCell(map.get("source")).getStringCellValue());
		}if(null != map.get("cd_num")){
			book.set("cd_num", row.getCell(map.get("cd_num")).getStringCellValue());
		}if(null != map.get("ocd_num")){
			book.set("ocd_num", row.getCell(map.get("ocd_num")).getStringCellValue());
		}if(null != map.get("files_num")){
			book.set("files_num", row.getCell(map.get("files_num")).getStringCellValue());
		}if(null != map.get("data_size")){
			book.set("data_size", row.getCell(map.get("data_size")).getStringCellValue());
		}if(null != map.get("receipt_no")){
			book.set("receipt_no", row.getCell(map.get("receipt_no")).getStringCellValue());
		}if(null != map.get("item_code")){
			book.set("item_code", row.getCell(map.get("item_code")).getStringCellValue());
		}if(null != map.get("photo_title")){
			book.set("photo_title", row.getCell(map.get("photo_title")).getStringCellValue());
		}if(null != map.get("shooting_date")){
			book.set("shooting_date", row.getCell(map.get("shooting_date")).getStringCellValue());
		}if(null != map.get("location")){
			book.set("location", row.getCell(map.get("location")).getStringCellValue());
		}if(null != map.get("poster")){
			book.set("poster", row.getCell(map.get("poster")).getStringCellValue());
		}if(null != map.get("create_year")){
			book.set("create_year", row.getCell(map.get("create_year")).getStringCellValue());
		}if(null != map.get("production_unit")){
			book.set("production_unit", row.getCell(map.get("production_unit")).getStringCellValue());
		}
		book.set("state", "A");
	}
	
	/**
	 * 删除单个文件
	 * @param sPath
	 */
	public void deleteFile(String sPath) {  
	    File file = new File(sPath);  
	    // 路径为文件且不为空则进行删除  
	    if (file.isFile() && file.exists()) {  
	        file.delete();    
	    }  
	} 
	
	public void deleteDirectory(String sPath) {  
	    //如果sPath不以文件分隔符结尾，自动添加文件分隔符  
	    if (!sPath.endsWith(File.separator)) {  
	        sPath = sPath + File.separator;  
	    }  
	    File dirFile = new File(sPath);  
	    //如果dir对应的文件不存在，或者不是一个目录，则退出  
	    if (!dirFile.exists() || !dirFile.isDirectory()) {  
	        return;  
	    }   
	    //删除文件夹下的所有文件(包括子目录)  
	    File[] files = dirFile.listFiles();  
	    for (int i = 0; i < files.length; i++) {  
	        //删除子文件  
	        if (files[i].isFile()) {  
	            deleteFile(files[i].getAbsolutePath());  
	        } //删除子目录  
	        else {  
	            deleteDirectory(files[i].getAbsolutePath());   
	        }  
	    }  
	    //删除当前目录  
	    dirFile.delete();
	} 
	
	/**
	 * 档案查询
	 */
	public void all(){
		Map<String,Object> pagemap = new HashMap<String, Object>();
		String tabid = getPara("tabid");
		Suser user = getSessionAttr("user");
		String file_number = getPara("file_number" , "");
		String flow_code = getPara("flow_code" , "");
		String title = getPara("title" , "");
		Integer pageNumber = getParaToInt("pageCurrent", 1);//要访问第几页，默认1
		Integer pageSize = getParaToInt("pageSize", 100);//每页多少条数据，默认15
		String orderField = getPara("orderField", "ctime");//对什么字段排序，默认空字符串
		String orderDirection = getPara("orderDirection", "desc");//升序还是降序，默认升序
		List<Srole> ls = roleService.getRoleFromUser(user.getStr("id"));
		String role = "";
		if (null != ls && ls.size() >0){
			role = ls.get(0).getStr("text");
		}
		bookService.findAllDoc(role , flow_code , title, file_number, 
				 pageNumber, pageSize, orderField, orderDirection, pagemap);
		setAttr("title", title);
		setAttr("file_number", file_number);
		setAttr("flow_code", flow_code);
		setAttr("tabid", tabid);
		List<Book> lb = (ArrayList)pagemap.get("pageData");
		if (null != lb && lb.size() > 0){
			for (Book b:lb){
				Suser usert = Suser.me.findById(b.get("creater"));
				if (null != usert){
					b.set("creater", usert.get("realname"));
				}
			}
		}
		setAttr("page",pagemap);
		setAttr("role", role);
		render("/admin/book/all.html");	
	}	
	
	/**
	 * 已完成档案
	 */
	public void completed(){
		Map<String,Object> pagemap = new HashMap<String, Object>();
		String tabid = getPara("tabid");
		Suser user = getSessionAttr("user");
		String file_number = getPara("file_number" , "");
		String flow_code = getPara("flow_code" , "");
		String title = getPara("title" , "");
		Integer pageNumber = getParaToInt("pageCurrent", 1);//要访问第几页，默认1
		Integer pageSize = getParaToInt("pageSize", 100);//每页多少条数据，默认15
		String orderField = getPara("orderField", "ctime");//对什么字段排序，默认空字符串
		String orderDirection = getPara("orderDirection", "desc");//升序还是降序，默认升序
		bookService.findAllCompleted(user.getStr("id") , flow_code , title, file_number, 
				 pageNumber, pageSize, orderField, orderDirection, pagemap);
		setAttr("title", title);
		setAttr("file_number", file_number);
		setAttr("flow_code", flow_code);
		setAttr("tabid", tabid);
		setAttr("page",pagemap);
		render("/admin/book/completed.html");	
	}	
	
	/**
	 * 生成1~10000之间的随机数
	 * @return
	 */
	public static int randomInt(){
    	int max=10000;
    	int min=1;
    	Random random = new Random();
    	int s = random.nextInt(max)%(max-min+1) + min;
    	return s;
	}
	
	public static Map<String, String> JsonToMap2(Model<Book> model) {
	    String jmodel = model.toJson();
	    Gson gson = new Gson();
	    Map<String, String> jsonmap = gson.fromJson(jmodel,
	        new TypeToken<Map<String, String>>() {
	    }.getType());
	    return jsonmap;
	}
}
